package com.jneagle.xlstool.bzwgjmx.view.gui;

import com.dwarfeng.dutil.basic.gui.swing.MappingTableModel;
import com.dwarfeng.dutil.basic.gui.swing.SwingUtil;
import com.jneagle.xlstool.bzwgjmx.stack.bean.entity.StructuredErrorInfo;
import com.jneagle.xlstool.bzwgjmx.stack.service.StructuredErrorInfoMaintainService;
import com.jneagle.xlstool.bzwgjmx.view.handler.I18nHandler;
import com.jneagle.xlstool.bzwgjmx.view.handler.MessageHandler;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.List;

/**
 * 结构化数据导入错误对话框。
 *
 * @author DwArFeng
 * @since 1.0.0
 */
@Component
@DependsOn("viewConfiguration")
public class StructuredDataErrorDialog extends JDialog {

    private static final long serialVersionUID = -757209255042898204L;

    private final ApplicationContext ctx;

    private final StructuredErrorInfoMaintainService structuredErrorInfoMaintainService;

    private final I18nHandler i18nHandler;
    private final ThreadPoolTaskExecutor executor;
    private final MessageHandler messageHandler;

    private final MappingTableModel<StructuredErrorInfo> structuredErrorInfoTableModel;

    private final JTable structuredImportErrorInfoTable = new JTable();

    public StructuredDataErrorDialog(
            ApplicationContext ctx,
            StructuredErrorInfoMaintainService structuredErrorInfoMaintainService,
            I18nHandler i18nHandler,
            ThreadPoolTaskExecutor executor,
            MessageHandler messageHandler,
            @Qualifier("structuredErrorInfoTableModel") MappingTableModel<StructuredErrorInfo> structuredErrorInfoTableModel
    ) {
        this.ctx = ctx;
        this.structuredErrorInfoMaintainService = structuredErrorInfoMaintainService;
        this.i18nHandler = i18nHandler;
        this.executor = executor;
        this.messageHandler = messageHandler;
        this.structuredErrorInfoTableModel = structuredErrorInfoTableModel;
    }

    @SuppressWarnings("DuplicatedCode")
    @PostConstruct
    public void postConstruct() {
        // 初始化窗口。
        setTitle(i18nHandler.getMessage("ui.frame.001"));
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 600, 450);
        setModal(true);
        // 初始化 contentPanel。
        JPanel contentPanel = new JPanel();
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPanel);
        contentPanel.setLayout(new BorderLayout(0, 0));
        // 向 contentPanel 中添加表格组件。
        I18nTableCellRenderer structuredImportErrorInfoHeaderRender = ctx.getBean(
                I18nTableCellRenderer.class,
                ctx.getBean(I18nHandler.class),
                structuredImportErrorInfoTable.getTableHeader().getDefaultRenderer()
        );
        structuredImportErrorInfoTable.getTableHeader().setDefaultRenderer(structuredImportErrorInfoHeaderRender);
        structuredImportErrorInfoTable.setModel(structuredErrorInfoTableModel);
        structuredImportErrorInfoTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        TableColumn resizingColumn = structuredImportErrorInfoTable.getColumnModel().getColumn(0);
        structuredImportErrorInfoTable.getTableHeader().setResizingColumn(resizingColumn);
        TableColumn i18nColumn = structuredImportErrorInfoTable.getColumnModel().getColumn(1);
        I18nTableCellRenderer i18nColumnRender = ctx.getBean(
                I18nTableCellRenderer.class,
                ctx.getBean(I18nHandler.class),
                new DefaultTableCellRenderer()
        );
        i18nColumn.setCellRenderer(i18nColumnRender);
        resizingColumn.setPreferredWidth(80);
        JScrollPane structuredImportErrorInfoScrollPane = new JScrollPane();
        structuredImportErrorInfoScrollPane.setViewportView(structuredImportErrorInfoTable);
        add(structuredImportErrorInfoScrollPane, BorderLayout.CENTER);

        // 添加侦听器。

        // 同步属性。
        syncProperties();
    }

    private void syncProperties() {
        SwingUtil.invokeInEventQueue(() -> {

        });
    }

    @PreDestroy
    public void preDestroy() {
        // 移除侦听器。

    }

    public void loadData() {
        structuredErrorInfoTableModel.clear();
        executor.submit(() -> {
            try {
                List<StructuredErrorInfo> structuredErrorInfoList =
                        structuredErrorInfoMaintainService.lookupAsList();
                structuredErrorInfoTableModel.addAll(structuredErrorInfoList);
            } catch (Exception e) {
                messageHandler.error(this, "ui.label.031");
            }
        });
    }
}
